package ssi.tools;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import ssi.tools.Connector;

public class ShowAnimals {
		
	public static ArrayList<String> showNewAnimals(){
		ArrayList<String>animals = new ArrayList<String>();
		try{
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
			ResultSet result = statement.executeQuery("SELECT * FROM `animals` order by id desc limit 1");
			while (result.next()) {
				String name=result.getString("name");
				String addDate=result.getString("addDate");
				int id=Integer.parseInt(result.getString("id"));
				String image=result.getString("images");
				animals.add("<div class='rp_box'><a href='index.jsp?page=animalProfile&id="+id+"'><img src='images/animals/"+image+"' class='image_frame_80' /></a><div class='date'>"+addDate+"</div><br><br><a href='index.jsp?page=animalProfile&id="+id+"'><p class='zwierzaki'>"+name+"</p></a><div class='cleaner'></div>");	
			}
			result.close();
			connection.close();
		}catch(Exception e){
			animals.add("Wystapil blad podczas proby pobrania zwierzakow z bazy");
		}
		return animals;
	}
	
	public static ArrayList<String> showAnimals(){
		ArrayList<String>animals = new ArrayList<String>();
		try{
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
			ResultSet result = statement.executeQuery("SELECT * FROM `animals` order by id desc limit 5");
			while (result.next()) {
				String name=result.getString("name");
				String breed = result.getString("breed");
				String addDate=result.getString("addDate");
				String birthDate=result.getString("birthDate");
				int id=Integer.parseInt(result.getString("id"));
				String image=result.getString("images");
				animals.add("<div class='rp_box'><img src='images/animals/"+image+ "' class='image_frame_100' /><div class='date'>"+addDate+"</div><p><em><div class='comment more'><h3>"+breed+"</h3><ul><li>Imie: "+name+"</li><li>Data urodzenia: "+birthDate+"</li></ul></div></em> <a href='index.jsp?page=animalProfile&id="+id+"'>Przejdz do profilu zwierzaka!</a></p></em></p><div class='cleaner'></div></div>");	
			}
			result.close();
			connection.close();
		}catch(Exception e){
			animals.add("Wystapil blad podczas proby pobrania zwierzakow z bazy");
		}
		return animals;
	}
	
	public static ArrayList<String> showAdopted(int id){
		ArrayList<String>animals = new ArrayList<String>();
		try{
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
			ResultSet result = statement.executeQuery("SELECT * FROM adoption, animals WHERE AnimalId=animals.id AND customerID="+id);
			while (result.next()) {
				animals.add("<tr><td><img src='images/animals/"+result.getString("images")+ "' class='image_frame_80' /></td><td><a href=\"/SSIProject/index.jsp?page=animalProfile&id="+result.getInt(5)+"\">"+result.getString("breed")+" - "+result.getString("name")+"</a></td></tr></br>");	
			}
		}catch(Exception e){
			animals.add("Wystapil blad podczas proby pobrania zwierzakow z bazy");
		}
		return animals;
	}
	
	public static ArrayList<String> showFavourites(int id){
		ArrayList<String>animals = new ArrayList<String>();
		try{
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
			System.out.println("SELECT * FROM favourites, animals WHERE animalId=animals.id AND userId="+id);
			ResultSet result = statement.executeQuery("SELECT * FROM favourites, animals WHERE animalId=animals.id AND userId="+id);
			while (result.next()) {
				animals.add("<tr><td><img src='images/animals/"+result.getString("images")+ "' class='image_frame_80' /></td><td><a href=\"/SSIProject/index.jsp?page=animalProfile&id="+result.getInt(3)+"\">"+result.getString("breed")+" - "+result.getString("name")+"</a></td></tr></br>");	
			}
		}catch(Exception e){
			animals.add("Wystapil blad podczas proby pobrania zwierzakow z bazy");
		}
		return animals;
	}
}
